﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ZQNB.Common.Caching;
using ZQNB.Common.Ioc;

namespace ZQNB.Web.Common.Contexts
{
    public class NbRequest
    {
        public NbRequest() : this("", "", "", "", "", "", "") { }

        public NbRequest(string identityName, string domainUri,string site, string area, string controller, string action, string owner)
        {
            IdentityName = identityName ?? "";
            DomainUri = domainUri ?? "";
            Area = area ?? "";
            Site = site ?? "";
            Controller = controller ?? "";
            Action = action ?? "";
            Owner = owner ?? "";
        }

        public string Site { get; set; }
        public string Area { get; set; }
        public string Controller { get; set; }
        public string Action { get; set; }
        public string Owner { get; set; }
        public string QueryString { get; set; }
        public string IdentityName { get; set; }
        public string DomainUri { get; set; }

        public override string ToString()
        {
            return string.Format("{0},{1},{2},{3},{4},{5}, {6}", DomainUri, Site, Area, Owner, Controller, Action, IdentityName);
        }
    }

    /// <summary>
    /// 一个来自客户端的请求
    /// 发生在ActionExecuting时
    /// 迟加载，同一个请求管道中，第一次访问进行计算，后面使用缓存
    /// </summary>
    public interface INbRequestContext
    {
        NbRequest GetCurrentNbRequest();

        void SetPassValue<T>(string key, T value);
        T GetPassValue<T>(string key);
    }

    public class NbRequestContext : INbRequestContext
    {
        private readonly IPerRequestCacheManager _perRequestCacheManager;

        public NbRequestContext(IPerRequestCacheManager perRequestCacheManager)
        {
            _perRequestCacheManager = perRequestCacheManager;
        }

        //关于此静态属性的作用，参照注释中的使用DEMO
        public static INbRequestContext Current
        {
            get
            {
                return CoreServiceProvider.LocateService<INbRequestContext>();
            }
        }
        public NbRequest GetCurrentNbRequest()
        {
            throw new NotImplementedException();
        }

        public void SetPassValue<T>(string key, T value)
        {
            _perRequestCacheManager.Set(key, value);
        }

        public T GetPassValue<T>(string key)
        {
            T value = _perRequestCacheManager.Get<T>(key);
            return value;
        }

        #region demo for replace impl

        //////Kernel.Bind<INbRequestContextProvider>().To<NbRequestContextProvider>();
        ////Kernel.Bind<INbRequestContextProvider>().To<AnotherNbRequestContextProvider>();
        ////通过构造函数注入INbRequestContextProvider和NbRequestContextProvider.Current得到的是同一个是实现（AnotherNbRequestContextProvider）
        ////Type type = NbRequestContextProvider.Current.GetType();
        ////Type type1 = _nbRequestContextProvider.GetType();
        ////bool sameType = type == type1; //true
        //public class AnotherNbRequestContextProvider : INbRequestContextProvider
        //{
        //    public NbRequest GetCurrentNbRequest()
        //    {
        //        throw new NotImplementedException();
        //    }
        //}

        #endregion
    }
}
